<template>
  <ul class="app-header-nav">
    <li class="home"><RouterLink to="/">首页</RouterLink></li>
    <li v-for="item in list" :key="item.id">
      <a href="#" @click="fns(item.id)">{{ item.name }}</a>
      <!-- 新增++++++++ -->
      <div v-if="loading" class="layer">
        <ul>
          <li v-for="i in item.children" :key="i">
            <a @click="fn2(i.id)" href="#">
              <img :src="i.picture" alt="" />
              <p>{{ i.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <div class="layer loading" v-else>加载中...</div>
      <!-- 新增++++++++ -->
    </li>
  </ul>
</template>

<script>
export default {
  name: "AppHeaderNav",
  data() {
    return {
      loading: true,
      list: this.$store.state.category.list,
      time: "",
      id: "",
    };
  },
  async created() {
    this.loading = false;
    await this.$store.dispatch("category/getList");
    this.list = this.$store.state.category.list;
    this.loading = true;
  },
  methods: {
    fns(id) {
      const data = new Date().valueOf() - this.time;
      if ((data < 1000) & (id === this.id)) {
        return;
      }
      this.id = id;
      this.time = new Date().valueOf();
      this.$router.push(`/category/${id}`);
    },
    fn2(id) {
      const data = new Date().valueOf() - this.time;
      if ((data < 1000) & (id === this.id)) {
        return;
      }
      this.id = id;
      this.time = new Date().valueOf();
      this.$router.push(`/category/sub/${id}`);
    },
  },
};
</script>

<style scoped lang="less">
.app-header-nav {
  width: 820px;
  display: flex;
  padding-left: 40px;
  position: relative;
  z-index: 998;
  > li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
    > a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }
    &:hover {
      > a {
        color: @xtxColor;
        border-bottom: 1px solid @xtxColor;
      }
      > .layer {
        height: 132px;
        opacity: 1;
      }
    }
  }
}
.loading {
  height: 100%;
  padding-top: 50px;
  font-size: 24px;
  font-weight: 600;
  text-align: center;
}
.layer {
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  transition: all 0.2s 0.1s;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 60px;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
